home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’90 / Restart Test Hack / ResTest.c < prev   
Encoding:
C/C++ Source or Header  |  1990-06-14  |  3.5 KB  |  164 lines  |  [TEXT/KAHL]

  1.  
  2. /* 
  3.     this is a little test of a restart scheme 
  4.     to give the user a short time to restart or
  5.     quit the application and then aautomatically restart
  6.     Mary Lynn Samford
  7. */
  8. static    long    excSave[2];
  9. pascal void MyRestart();
  10.  
  11. main()
  12.  
  13. {
  14.     Rect        r;
  15.     long        *lPtr;
  16.     WindowPtr    myWindow;
  17.     
  18.     EventRecord                myEvent;
  19.  
  20.     InitGraf(&thePort);
  21.     InitFonts();
  22.     InitWindows();
  23.     TEInit();
  24.     InitDialogs(0L);
  25.     InitCursor();
  26.  
  27. /*    save away the original bus & address error vectors 
  28.     (location 8 & 12 ) &
  29.     put in our new ( address of MyRestart) 
  30. */
  31.  
  32.     excSave[0] = *(long*)8;
  33.     excSave[1] = *(long*)12;
  34.     *(long*)8 = (long)(&MyRestart);
  35.     *(long*)12 = (long)(&MyRestart);
  36.     
  37. /* make a stupid window for this demo */
  38.  
  39.     myWindow = GetNewWindow(128,0l,-1L);
  40.     SetPort(myWindow);
  41.     SetRect(&r,60,60,200,200);
  42.     TextBox("This is a little program to test a restart scheme. Press a 'b' \
  43. to cause a bus error or a 'q' to quit.",101,&r,2);
  44.     
  45.     while (1)
  46.         if ( WaitNextEvent(everyEvent, &myEvent, 10L, 0L)) {
  47.             switch ( myEvent.what) {
  48.               case keyDown:
  49.                 switch ( myEvent.message & charCodeMask ) {
  50.                     case    'b':
  51.                         /* force a bus error */
  52.                         asm{
  53.                             move 0xfffffffe,a0
  54.                             jmp (a0)
  55.                         }
  56.                     case    'q':
  57.                         *(long*)8= excSave[0];
  58.                         *(long*)12 = excSave[1];
  59.                         ExitToShell();                /* crude & rude  */
  60.                 }
  61.                 break;
  62.               case app4Evt:                        /* multi-finder event */
  63.                 if (BitAnd(myEvent.message,1)) {   /* activate */
  64.                     excSave[0] = *(long*)8;
  65.                     excSave[1] = *(long*)12;
  66.                     *(long*)8 = (long)(&MyRestart);
  67.                     *(long*)12 = (long)(&MyRestart);
  68.                 }
  69.                 else {                    /* deactivate */
  70.                     *(long*)8= excSave[0];
  71.                     *(long*)12 = excSave[1];
  72.                 }
  73.                 break;    
  74.             case    updateEvt:
  75.                 myWindow = (WindowPtr) myEvent.message;
  76.                 BeginUpdate(myWindow);
  77.                     SetRect(&r,60,60,200,200);
  78.                     TextBox("This is a little program to test a restart scheme. Press a 'b' \
  79. to cause a bus error or a 'q' to quit.",101,&r,2);
  80.             
  81.                  EndUpdate(myWindow);
  82.                 break;        
  83.         
  84.         }
  85.     }
  86. }
  87.  
  88. CatPStr(to, from)
  89. char    *to,*from;
  90. {
  91.     int    i;
  92.     
  93.     for(i = 1; i <= from[0]; i++) {
  94.         to[to[0]+i] = from[i];
  95.     } 
  96.     to[0] += i-1;
  97. }
  98.  
  99. #define    MINCOUNT    1
  100.  
  101.  
  102. pascal void MyRestart()
  103. {
  104.     DialogPtr    dp,theDialog;
  105.     long        finalTC;
  106.     EventRecord                myEvent;
  107.     int        item,type,min=0,sec=0;
  108.     char    time_string[100],tempstr[20];
  109.     Handle    itemHandle;
  110.     Rect    r;
  111.     DateTimeRec    date;
  112.     
  113.     
  114.     finalTC = TickCount() + (60*60*MINCOUNT);        /* MINCOUNT minutes in the future */ 
  115.     dp = GetNewDialog (10000,0L,-1L);        
  116.     if ( dp == (DialogPtr)0)            /* no Dialog ?   Quit */
  117.         asm{
  118.             move.l ROMBase,A0
  119.             jmp 0x0a(A0)
  120.         }
  121.         
  122.     GetDItem (dp, 4 ,&type, &itemHandle,&r);
  123.     if ( itemHandle == (Handle)0)            /* no item ?   Quit */
  124.         asm{
  125.             move.l ROMBase,A0
  126.             jmp 0x0a(A0)
  127.         }
  128.     ShowWindow(dp);
  129.     *(long*)8= excSave[0];                /* restore original error handler */
  130.     *(long*)12 = excSave[1];            /* restore original error handler */
  131.     while(TickCount() < finalTC ) {
  132.         Secs2Date ((finalTC-TickCount())/60,&date);
  133.         if (date.minute != min || date.second != sec) {
  134.             min = date.minute;
  135.             sec = date.second;
  136.             NumToString (date.minute, time_string);
  137.             CatPStr(time_string,"\P Minutes ");
  138.             NumToString (date.second, tempstr);
  139.             CatPStr(time_string,tempstr);
  140.             CatPStr(time_string,"\P Seconds ");
  141.             SetIText (itemHandle,time_string);
  142.         }
  143.         WaitNextEvent(everyEvent, &myEvent, 10L, 0L);
  144.         if (IsDialogEvent (&myEvent)){
  145.             if (DialogSelect (&myEvent, &theDialog, &item) && theDialog == dp) {
  146.                 switch (item) {
  147.                     case 1:
  148.                         asm{
  149.                             move.l ROMBase,A0
  150.                             jmp 0x0a(A0)
  151.                         }
  152.                     case 2:
  153.                         ExitToShell();
  154.                 }    
  155.             }
  156.         }     
  157.     }
  158.     asm{
  159.         move.l ROMBase,A0
  160.         jmp 0x0a(A0)
  161.     }
  162. }
  163.  
  164.